<?php
//商品编辑模块
class GoodsAttrEditModel extends Model{
	public $gid;
//查找商品类型的普通的属性
	public function GoodsAttr($gid){
		$this->gid=$gid;
 		$cid=Q("cid",0,"intval");
 		//查找所属商品类型
 		$gtid=M("category")->where(array("cid"=>$cid))->find("gtid");
 		//查找商品类型里面的属性
 		$attrClass=M("g_attr_class")->where(array("add_goods_type_aid"=>$gtid['gtid'],'is_spec'=>2))->all();
 		foreach($attrClass as $n=>$attr){
 			$attrValue=M("g_arr_value")->where(array("g_attr_class_ac_id"=>$attr["ac_id"]))->all();
 			$show_type=$attr['show_type'];
 			if(method_exists($this,$show_type)){
 				$html=$this->$show_type($attrValue);
 			}
 			$attrClass[$n]['html']=$html;
 		}
 			return $attrClass;
	}

	//获得input型的属性值
	public function input($attrValue){
		//获得当前商品的属性值
		$goodsData=M("goods_attr")->where(array('gid'=>$this->gid))->all();
		//循环当前商品的属性值，当属性值的id等于主键的时候记录
		foreach($goodsData as $goods){
			if($goods['avid']==$attrValue[0]['av_id']){
			$html="<input type='text' name='attr[{$attrValue[0]['g_attr_class_ac_id']}|{$attrValue[0]['av_id']}]'  value='{$goods['value']}'>";
			}
		}
		return $html;
	}
	//规格属性值类型是box
	public function box($attrValue){
             $GoodsAttrData=M("goods_attr")->where(array("gid"=>$this->gid))->all();
             // p($GoodsAttrData);die;
		$html="<select name='attr[]'>";
             	//将属性值组合成一个下拉框
		foreach($attrValue as $attr){
			// p($attr);die;
			foreach($GoodsAttrData as $goods){
				if($goods['avid']==$attr['av_id']){
					$html.="<option value='{$attr['av_id']}|{$attr['av_name']}' selected=''>{$attr['av_name']}</option>";
					continue 2;//跳出两层循环
				}
			}

			$html.="<option value='{$attr['av_id']}|{$attr['av_name']}'>{$attr['av_name']}</option>";
		}
             $html.="</select>";
	   return $html;
	}






}